<template>
	<view v-if="detailList.goods_name">
		<!-- 轮播图 -->
			<view class="detail_swiper">
				<swiper :indicator-dots="true">
					<swiper-item v-for="(item,index) in detailList.pics" :key="item.pics_id">
						<view class="swiper-item">
							<image :src="item.pics_big_url" mode="" @click="preview(index)"></image>
						</view>
					</swiper-item>
				</swiper>
			</view>
			<!-- 信息 -->
			<view class="detail_box">
				<view class="detail_price">
					$ {{detailList.goods_price}}
				</view>
				<view class="detail_title">
					<view class="goods_name">
						{{detailList.goods_name}}
					</view>
					<view class="shou">
						<uni-icons type="star" size="18" color="geay"></uni-icons>
						<text>收藏</text>
					</view>
				</view>
				<view class="yf">
					快递：免运费 
				</view>
			</view>
			<view class="container">
				<rich-text :nodes="detailList.goods_introduce"></rich-text>
			</view>
			<view class="detail_nav">
				<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
			</view>
	</view>
</template>

<script>
	import {mapState,mapGetters} from 'vuex'
	export default {
		computed:{
			...mapGetters(['total'])
		},
		data() {
			return {
				goodsid:'',
				detailList:{},
				// 底部导航栏左侧按钮配置
				options:[
				{
					icon:'shop',
					text:'店铺'
				},
				{
					icon:'cart',
					text:'购物车',
					info:0
				}
				],
				// 地图导航栏有右侧按钮配置
				buttonGroup:[
					{
						text:'加入购物车',
						backgroundColor:'#ff0000',
						color:'#fff'
					},
					{
						text:'立即购买',
						backgroundColor:'#ffa200',
						color:'#fff'
					}
				]
			};
		},
		onLoad(options){
			this.goodsid=options.goods_id
			this.getDetail()
			
		},
		methods:{
			getDetail(){
				this.request({
					url:`/public/v1/goods/detail`,
					data:{goods_id:this.goodsid}
				}).then(({data:res})=>{
					console.log(res)
					res.message.goods_introduce=res.message.goods_introduce.replace(/webp/g,'jpg')
					this.detailList=res.message
				})
			},
			//点击轮播吐预览图片
			preview(index){
				uni.previewImage({
					current:index,
					urls:this.detailList.pics.map(x=>x.pics_big),
					loop:true
				})
			},
			// 底部导航栏左侧按钮点击事件
			onClick(e){
				console.log(e)
				if(e.content.text==="购物车"){
					uni.switchTab({
						url:'/pages/cart/cart'
					})
				}
			},
			// 底部导航栏右侧按钮点击事件
			buttonClick(e){
				if(e.index=='0'){
					const goods={
						goods_id:this.detailList.goods_id,
						goods_name:this.detailList.goods_name,
						goods_price:this.detailList.goods_price,
						goods_count:1,
						goods_number:this.detailList.goods_number,
						goods_big_logo:this.detailList.goods_big_logo,
						goods_state:true
					}
					this.$store.commit('ADDTOCART',goods)
				}
			}
		},
		watch:{
			total:{
				handler(newvalue){
				const findResult=this.options.find(x=>x.text==='购物车')
				if(findResult){
					findResult.info=newvalue
				}
				},
				immediate:true
			}
			
		}
	}
</script>

<style lang="scss">
swiper{
	height: 750rpx;
}
.swiper-item{
	height: 750rpx;
	image{
		width: 100%;
		height: 100%;
	}
}
.detail_box{
	padding: 10px 5px;
	.detail_price{
		padding: 10px 0;
		color: #c00000;
		font-size: 16px;
	}
	.detail_title{
		display: flex;
		justify-content: space-between;
		.goods_name{
			font-size: 13px;
			padding: 0 5px;
		}
		.shou{
			width: 120px;
			font-size: 12px;
			display: flex;
			flex-direction: column;
			align-items: center;
				left: 1px solid #efefef;
		}
	}
	.yf{
		font-size: 12px;
		color: gray;
		margin: 10px 0;
	}
}
.detail_nav{
	position: sticky;
	bottom: 0;
	height: 50px;
}
.container{
	padding-bottom:50px ;
}
</style>
